// SSMonitorItems.h: interface for the CSSMonitorItems class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SSMONITORITEMS_H__FB181DB0_B87D_4B62_9DBD_B8526FCAE396__INCLUDED_)
#define AFX_SSMONITORITEMS_H__FB181DB0_B87D_4B62_9DBD_B8526FCAE396__INCLUDED_

#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <string>
#include <map>
#include "../../source/OS/OSMutex.h"

#ifndef WIN32
	#include <typeinfo>
#endif
using std::string;
using std::map;

#define STAT_COUNT_INC(pThis) \
{\
char szItemName[64] = {0};\
sprintf( szItemName, "%s total", typeid(*pThis).name() );\
CSSMonitorItems::Instance().IncCount( szItemName );\
}
#define STAT_COUNT_DEC(pThis) \
{\
	char szItemName[64] = {0};\
	sprintf( szItemName, "%s total", typeid(*pThis).name() );\
	CSSMonitorItems::Instance().DecCount( szItemName );\
}

#define STAT_RTSP_CONN_TOTAL		"rtsp connect total"
#define STAT_RSTP_CONN_TOTAL		"rstp connect total"
#define STAT_THREAD_TOTAL			"thread total"
#define STAT_THREAD_RUN_TOTAL		"run thread total"

#define STAT_OUTPUT_TITLE			"              item name               | current val |     accumulative val\r\n--------------------------------------|-------------|----------------------\r\n"
#ifdef WIN32
	#define STAT_RESULT_OUTPUT_FORMAT "%s| %10u  | %20I64u\r\n"
#else
	#define STAT_RESULT_OUTPUT_FORMAT "%s| %10u  | %20llu\r\n"
#endif

class CSSMonitorItems  
{
	typedef struct tagStatItem
	{
		UInt64 uTotal;
		UInt32 uCurrCount;
		tagStatItem()
		{
			uTotal = 0;
			uCurrCount = 0;
		}
		void Reset()
		{
			uTotal = 0;
			uCurrCount = 0;
		}
	}StatItem,*PStatItem;
	typedef map<string,PStatItem>			MAP_STR_TO_STATITEM;
	typedef MAP_STR_TO_STATITEM::iterator	MAP_ITER_STR_TO_STATITEM;
public:
	static CSSMonitorItems& Instance();
	virtual ~CSSMonitorItems();
	void Reset();
	void IncCount( const string& strItemName );
	void DecCount( const string& strItemName );
	UInt32 GetCount( const string& strItemName );
public:
	void InitEnumerator();
	void ReleaseEnumerator();
	Bool EnumStatItem( string& strItemName, UInt32& uCurrCount, UInt64& uTotal );
private:
	CSSMonitorItems();
	void ModifyCount( const string& strItemName, Bool bIsAdd );
	string AdjustItemNameToFitLen( const string& strItemName );
	
	OSMutex m_mtxMap;
	MAP_STR_TO_STATITEM m_mapStatic;
	MAP_ITER_STR_TO_STATITEM m_iterTrace;

	static CSSMonitorItems s_Instance;
};

#endif // !defined(AFX_SSMONITORITEMS_H__FB181DB0_B87D_4B62_9DBD_B8526FCAE396__INCLUDED_)
